﻿using System;
using System.IO;
using System.Text;
using System.Web.Mvc;
using MvcApplication6.Models;

namespace MvcApplication6.Controllers
{
    public class Default1Controller : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Index2()
        {
            return Json(
                new[]
                    {
                        GetHtmlUpdateInfo(
                            "div1",
                            "_PartialPage1",
                            new PartialPage1ModelEx(DateTime.Now))
                    }
                );
        }

        public ActionResult Index3()
        {
            return Json(
                new[]
                    {
                        GetHtmlUpdateInfo(
                            "div2",
                            "_PartialPage2",
                            new PartialPage2ModelEx(DateTime.Now))
                    }
                );
        }

        public ActionResult Index4()
        {
            return Json(
                new[]
                    {
                        GetHtmlUpdateInfo(
                            "div1",
                            "_PartialPage1",
                            new PartialPage1ModelEx(DateTime.Now)),
                        GetHtmlUpdateInfo(
                            "div2",
                            "_PartialPage2",
                            new PartialPage2ModelEx(DateTime.Now))
                    }
                );
        }

        private HtmlUpdateInfo GetHtmlUpdateInfo(string divId, string viewName, object model)
        {
            return new HtmlUpdateInfo(divId, RenderPartialViewToString(this, viewName, model));
        }

        protected static string RenderPartialViewToString(Controller it,  string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = it.ControllerContext.RouteData.GetRequiredString("action");

            it.ViewData.Model = model;

            var stringBuilder = new StringBuilder();
            using (var writer = new StringWriter(stringBuilder))
            {
                var viewEngineResult = ViewEngines.Engines.FindPartialView(it.ControllerContext, viewName);
                viewEngineResult.View.Render(
                    new ViewContext(
                        it.ControllerContext,
                        viewEngineResult.View,
                        it.ViewData,
                        it.TempData,
                        writer),
                    writer);
            }
            return stringBuilder.ToString();
        }
    }

    public class HtmlUpdateInfo
    {
        private readonly string divId;
        private readonly string html;

        public string DivId
        {
            get { return divId; }
        }

        public string Html
        {
            get { return html; }
        }

        public HtmlUpdateInfo(string divId, string html)
        {
            this.divId = divId;
            this.html = html;
        }
    }
}
